<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>函数限流</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <script src="https://unpkg.com/lodash@4.17.20/lodash.min.js"></script>
</head>
<body>
<div id="app">
    <button @click="click">点击</button>
</div>
</body>
<script>
    var throttle = false;

    function throttleTool(callback, timeout) {
        if (!throttle) {
            callback()
        } else {
            return
        }
        throttle = true;
        setTimeout(() => {
            throttle = false
        }, timeout)
    }

    const data = {
        // data() {
        //     return {
        //         threshold: false // 控制按钮是否被触发
        //     }
        // },
        methods: {
            // handleClick() {
            //     if (!this.threshold) {
            //         console.log(Date())
            //     } else {
            //         return
            //     }
            //     this.threshold = true; // 触发按钮
            //     setTimeout(() => { // 延时触发
            //         this.threshold = false;
            //     }, 2000);
            // }
            // handleClick() {
            //     throttleTool(() => {
            //         console.log(Date())
            //     }, 2000)
            // }
            // 使用 lodash 限流
            click: _.debounce(function () {
                console.log(Date())
            }, 2000)
        }
    }
    Vue.createApp(data).mount("#app")
</script>
</html>